package com.example.triprec.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.triprec.R;
import com.example.triprec.common.ScenicData;
import com.example.triprec.entity.Comment;
import com.example.triprec.entity.Scenic;

import java.util.List;

public class MyCommentAdapter extends RecyclerView.Adapter<MyCommentAdapter.ViewHolder> {

    private List<Comment> commentList;
    private OnItemClickListener onItemClickListener;

    public MyCommentAdapter(List<Comment> commentList) {
        this.commentList = commentList;
    }

    public void setData(List<Comment> commentList) {
        this.commentList = commentList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_comment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Comment comment = commentList.get(position);

        // 获取景点信息
        Scenic scenic = ScenicData.getScenicById(comment.getScenicId());
        if (scenic != null) {
            holder.tvScenicName.setText(scenic.getName());
            holder.tvScenicLocation.setText(scenic.getLocation());

            // 设置景点图片
            if (scenic.getImageResources() != null && scenic.getImageResources().length > 0) {
                holder.ivScenicImage.setImageResource(scenic.getImageResources()[0]);
            }
        }

        // 设置评论内容
        holder.tvCommentContent.setText(comment.getContent());
        holder.tvCommentTime.setText(comment.getCreateTime());

        // 设置评分
        if (comment.getScore() != null) {
            holder.ratingBar.setRating(comment.getScore().floatValue());
            holder.tvRatingValue.setText(String.valueOf(comment.getScore()));
            holder.ratingBar.setVisibility(View.VISIBLE);
            holder.tvRatingValue.setVisibility(View.VISIBLE);
        } else {
            holder.ratingBar.setVisibility(View.GONE);
            holder.tvRatingValue.setVisibility(View.GONE);
        }

        // 设置删除按钮点击事件
        holder.tvDelete.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onDeleteClick(comment, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return commentList == null ? 0 : commentList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivScenicImage;
        TextView tvScenicName;
        TextView tvScenicLocation;
        RatingBar ratingBar;
        TextView tvRatingValue;
        TextView tvCommentContent;
        TextView tvCommentTime;
        TextView tvDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivScenicImage = itemView.findViewById(R.id.iv_scenic_image);
            tvScenicName = itemView.findViewById(R.id.tv_scenic_name);
            tvScenicLocation = itemView.findViewById(R.id.tv_scenic_location);
            ratingBar = itemView.findViewById(R.id.rating_bar);
            tvRatingValue = itemView.findViewById(R.id.tv_rating_value);
            tvCommentContent = itemView.findViewById(R.id.tv_comment_content);
            tvCommentTime = itemView.findViewById(R.id.tv_comment_time);
            tvDelete = itemView.findViewById(R.id.tv_delete);
        }
    }

    public interface OnItemClickListener {
        void onDeleteClick(Comment comment, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
} 